<?php

namespace App\V3\V3_2_7\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SellPackageDetailModel extends Model
{


    use HasFactory;

    protected $table = 'sell_package_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    public static function detail($id)
    {
        return self::select(
        'sell_package_detail.id',
        'sell_package_detail.id as sell_package_id',
        'package_id',
        'package_name',
        'price',
        'total_cost',
        'sell_id',
        'num',
        'out_num',
        'package_no',
        'piece'
    )
        ->leftJoin('package as p', 'p.id', '=', 'sell_package_detail.package_id')
        ->with('packageContent', function($query) {
            $query->select(
                'sell_package_content.id',
                'sell_package_content.id as sell_package_content_id',
                'sell_package_content.materiel_class_id',
                'sell_package_content.storage_id',
                'sell_package_content.package_content_id',
                'sell_package_content.storage_manage_id',
                'sell_package_content.materiel_type_id',
                'package_detail_id',
                'mt.materiel_name',
                'mt.type_name',
                'mt.selling_price',
                'mt.materiel_no',
                'mt.materiel_img',
                'mt.cost',
                'mcs.class_name as s_class_name',
                'mcs.class_no as s_class_no',
                'mcf.class_name as f_class_name',
                'mcf.class_no as f_class_no',
                's.storage_name',
                'sm.in_storage_num',
                'sell_package_content.num',
                'sell_package_content.total_num',
                'sell_package_content.shelf_id',
                'sell_package_content.location_id',
                'se.shelf_name',
                'lo.location_name',
                'mt.is_use_serial'
            )
                ->with('packageBatchInfo', function($query) {
                    $query->select(
                        'sell_package_batch.id',
                        'sell_package_batch.sell_package_content_id',
                        'sell_package_batch.storage_manage_id',
                        'sell_package_batch.storage_batch_id',
                        'sell_package_batch.sell_id',
                        'sell_package_batch.main_id',
                        'sell_package_batch.num',
                        'sell_package_batch.rest_num',
                        'batch_no',
                    )
                        ->leftJoin('storage_batch as a', 'a.id', '=', 'sell_package_batch.storage_batch_id');
                })
                ->leftJoin('materiel_type as mt', 'mt.id', '=', 'sell_package_content.materiel_type_id')
                ->leftJoin('materiel_class as mcs', 'mcs.id', '=', 'sell_package_content.materiel_class_id')
                ->leftJoin('materiel_class as mcf', 'mcf.id', '=', 'mcs.parent_id')
                ->leftJoin('storage_manage as sm', 'sm.id', '=', 'sell_package_content.storage_manage_id')
                ->leftJoin('storage as s', 's.id', '=', 'sell_package_content.storage_id')
                ->leftJoin('storage_shelf as se','sell_package_content.shelf_id','=','se.id')
                ->leftJoin('storage_shelf_location as lo','sell_package_content.location_id','=','lo.id');
        })
            ->where('sell_id', $id)->get()->toArray();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'sell_date'
    ];

    public function packageContent()
    {
        return self::hasMany(SellPackageContentModel::class, 'package_detail_id', 'id');
    }

    public function packageBatch()
    {
        return self::hasMany(SellPackageBatchModel::class, 'sell_package_content_id', 'id');
    }
}
